/*
 * @Author: zh
 * @Date: 2024-07-29 09:14:41
 * @LastEditors: zh
 * @LastEditTime: 2024-11-18 11:25:03
 * @Description: 
 */
const anagrams = str => {

    if (str.length <= 2) return str.length === 2 ? [str, str[1] + str[0]] : [str];
    let  a =  str.split('').reduce((acc, letter, i) =>
    {
        // console.log(acc,"acc");
        // console.log(letter,"letter");
        // console.log(anagrams(str.slice(0, i) + str.slice(i + 1)).map(val => letter + val)) 
        //console.log(anagrams(str.slice(0, i) + str.slice(i + 1)),"合并");
        return acc.concat(anagrams(str.slice(0, i) + str.slice(i + 1)).map(val =>{
            console.log(letter,"letter");
            console.log(val,"val");
            console.log(letter + val,"letter + val");
            return letter + val
        } ));
    },[])
        // acc.concat(anagrams(str.slice(0, i) + str.slice(i + 1)).map(val => letter + val)), []);


    return a
  };
  
  // anagrams('abc') -> ['abc','acb','bac','bca','cab','cba']
  anagrams('abcd')